package com.antilia.infomaresme.data.dao.hibernate;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.antilia.infomaresme.data.dao.interfaces.IItemNewsDao;
import com.antilia.infomaresme.data.dataobjects.Item;
import com.antilia.infomaresme.data.dataobjects.ItemNews;
import com.antilia.infomaresme.data.dataobjects.VisibilityStatus;
/**
 * @author Ernesto
 *
 */
public class ItemNewsDaoHibernateImp extends AbstractDaoHibernateImpl<ItemNews> implements IItemNewsDao {

	public ItemNewsDaoHibernateImp()
	{
		super(ItemNews.class);
	}
	
	
	private void configureCriteria(Criteria criteria, VisibilityStatus status, Item item) {
		if(item != null) {
			criteria.add(Restrictions.eq("item", item));
		}
		if(status != null) {
			criteria.add(Restrictions.eq("status", status));
		}
	}


	@SuppressWarnings("unchecked")
	public List<ItemNews> getNews(Item item,VisibilityStatus status, int start, int size) {
		Criteria criteria = getSession().get().createCriteria(ItemNews.class);
		criteria.setFetchSize(size);
		criteria.setFirstResult(start);
		configureCriteria(criteria, status, item);
		return (List<ItemNews>)criteria.list();
	}


	public Long countNews(Item item, VisibilityStatus status) {
		Criteria criteria = session.get().createCriteria(ItemNews.class);
		criteria.setProjection(Projections.rowCount());
		configureCriteria(criteria, status, item);
		return (Long)(criteria.uniqueResult());
	}
}
